package com.bjc.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;

import com.google.common.base.Predicates;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {


	@Bean
	public Docket systemApi() {
		ParameterBuilder tokenPar = new ParameterBuilder();  
		List<Parameter> pars = new ArrayList<Parameter>();  
		tokenPar.name("bjcToken").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();  
//		ParameterBuilder tokenPar2 = new ParameterBuilder();  
//		tokenPar2.name("page-url").description("前台页面").modelRef(new ModelRef("string")).parameterType("header").required(false).build();  
        pars.add(tokenPar.build());  
//        pars.add(tokenPar2.build());  
		return new Docket(DocumentationType.SWAGGER_2).groupName("2.框架接口").genericModelSubstitutes(DeferredResult.class)
				.useDefaultResponseMessages(false).forCodeGeneration(true)
				.pathMapping("/").select() // 选择那些路径和api会生成document
				.apis(RequestHandlerSelectors.basePackage("com.bjc"))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()) // 对所有路径进行监控
				.paths(PathSelectors.regex("/api/cloud/.*")) // 对所有路径进行监控
				.build()
//				.directModelSubstitute(LocalDate.class, Date.class) //直接用提供的替代代替模型类，用Date替代LocalDate
//				.directModelSubstitute(LocalDate.class, java.sql.Date.class)
				.globalOperationParameters(pars)  
				.apiInfo(bjcBaseApiInfo());
	}

	@Bean
	public Docket bjcBaseApi() {
		List<Parameter> pars = new ArrayList<Parameter>();  
		ParameterBuilder tokenPar = new ParameterBuilder();  
		tokenPar.name("bjcToken").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();  
//		ParameterBuilder tokenPar2 = new ParameterBuilder();  
//		tokenPar2.name("page-url").description("前台页面").modelRef(new ModelRef("string")).parameterType("header").required(false).build();  
        pars.add(tokenPar.build());  
//        pars.add(tokenPar2.build()); 
		// pathMapping，最终调用接口后会和paths拼接在一起
		return new Docket(DocumentationType.SWAGGER_2).groupName("1.薪资系统接口")
				// .genericModelSubstitutes(ResponseEntity.class)
				.genericModelSubstitutes(DeferredResult.class).useDefaultResponseMessages(false)
				.forCodeGeneration(false).pathMapping("/").select().apis(RequestHandlerSelectors.basePackage("com.bjc"))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) // 被ApiOperation这个注解注解过的api接口
				.paths(PathSelectors.regex("/api/.*")) // 对所有路径进行监控
				.paths(Predicates.not(PathSelectors.regex("/api/cloud/.*"))) // 除去路径
				.paths(Predicates.not(PathSelectors.regex("/api/public/.*"))) // 除去路径
				.build()
//				.directModelSubstitute(LocalDate.class, Date.class) //直接用提供的替代代替模型类，用Date替代LocalDate
				.globalOperationParameters(pars)  
				.apiInfo(bjcBaseApiInfo());
	}
	
	@Bean
	public Docket pubBaseApi() {
		ParameterBuilder tokenPar = new ParameterBuilder();  
		List<Parameter> pars = new ArrayList<Parameter>();  
		tokenPar.name("bjcToken").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();  
//		ParameterBuilder tokenPar2 = new ParameterBuilder();  
//		tokenPar2.name("page-url").description("前台页面").modelRef(new ModelRef("string")).parameterType("header").required(false).build();  
        pars.add(tokenPar.build());  
//        pars.add(tokenPar2.build());  
		return new Docket(DocumentationType.SWAGGER_2).groupName("3.对外公开接口").genericModelSubstitutes(DeferredResult.class)
				.useDefaultResponseMessages(false).forCodeGeneration(true)
				.pathMapping("/").select() // 选择那些路径和api会生成document
				.apis(RequestHandlerSelectors.basePackage("com.bjc"))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()) // 对所有路径进行监控
				.paths(PathSelectors.regex("/api/public/.*")) // 对所有路径进行监控
				.build()
//				.directModelSubstitute(LocalDate.class, Date.class) //直接用提供的替代代替模型类，用Date替代LocalDate
//				.directModelSubstitute(LocalDate.class, java.sql.Date.class)
				.globalOperationParameters(pars)  
				.apiInfo(bjcBaseApiInfo());
	}
	
	@Bean
	public Docket mobileBaseApi() {
		ParameterBuilder tokenPar = new ParameterBuilder();  
		List<Parameter> pars = new ArrayList<Parameter>();  
		tokenPar.name("bjcToken").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();  
//		ParameterBuilder tokenPar2 = new ParameterBuilder();  
//		tokenPar2.name("page-url").description("前台页面").modelRef(new ModelRef("string")).parameterType("header").required(false).build();  
		pars.add(tokenPar.build());  
//        pars.add(tokenPar2.build());  
		return new Docket(DocumentationType.SWAGGER_2).groupName("4.移动端接口").genericModelSubstitutes(DeferredResult.class)
				.useDefaultResponseMessages(false).forCodeGeneration(true)
				.pathMapping("/").select() // 选择那些路径和api会生成document
				.apis(RequestHandlerSelectors.basePackage("com.bjc"))
				.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
				.apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()) // 对所有路径进行监控
				.paths(PathSelectors.regex("/api/mobile/.*")) // 对所有路径进行监控
				.build()
				.globalOperationParameters(pars)  
				.apiInfo(bjcBaseApiInfo());
	}
	

	private ApiInfo bjcBaseApiInfo() {
		return new ApiInfoBuilder().title("佰钧成接口管理平台")// 大标题
				.description("接口管理及测试")// 详细描述
				.version("1.0")// 版本
				.termsOfServiceUrl("NO terms of service").license("武汉佰钧成技术有限责任公司").licenseUrl("http://www.bill-jc.com/")
				.build();
	}

}
